/**
 * @(#) ContractProxy.as
 *
 * @date		2009. 03. 11.
 * @version		1.0
 * @author		Park, JuneHo
 * @description	 계약현황 조회 - Proxy
 * 				 
 */
package com.skt.common_hr.contract.model
{
	import com.skt.common_hr.common.Variables;
	import com.skt.common_hr.common.puremvc.CommonProxy;
	import com.skt.common_hr.contract.ContractFacade;
	import com.skt.common_hr.contract.model.vo.ContractVO;
	import com.wcl.dataloader.*;
	import com.wcl.utils.DateUtil;
	import com.wcl.utils.StringUtil;
	
	import mx.collections.ArrayCollection;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;

	public class ContractProxy extends CommonProxy
	{
		public static const NAME:String = "ContractProxy";
		
		public var contractList:ArrayCollection;


		/**
		 * 생성자
		 * 
		 * @param
		 * @return void
		 */
		public function ContractProxy(data:Object=null)
		{
			super(NAME, data);
		}
		
		
		/**
		 * data load를 수행
		 * 		- DataLoader 를 생성하고 Import Parameter 객체를 생성, RFC를 호출
		 * 
		 * @param pernr 사번
		 * @return void
		 */
		public function load(pernr:String):void
		{
			var param:Object = new Object();
			param["I_PERNR"] = Variables.myPernr;
			loadData("Z_HR_0017", param, result, fault); 
		}
		







		
		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Event Listeners
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		
		/**
		 * 서버에서 정상적으로 리턴된 결과를 처리
		 * 
		 * @param
		 * @return void
		 */
		public function result(event:ResultEvent):void
		{
			if(event.result["RETCODE"]==0) {
				this.contractList = parseContract(event.result.RTAB);
				facade.sendNotification(ContractFacade.LOAD_CONTRACT_COMPLETE, this);
			}
			else {
				trace( "failed to load 계약현황: (" +event.result["RETCODE"] + ") "+event.result["RETTEXT"]);
				facade.sendNotification(ContractFacade.LOAD_CONTRACT_FAILED, this);
			}
		}
		
		/**
		 * 서버에서 오류가 발생했을 처리
		 * 
		 * @param
		 * @return void
		 */
		private function fault(event:FaultEvent):void
		{
			trace("Fault message in ContractPRoxy: " + event.fault.faultString);
			facade.sendNotification(ContractFacade.LOAD_CONTRACT_FAILED, this);
		}
		
		
		
		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Data Parsing functions
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		 * 서버에서 전달된 result 객체를 분석하여 Value Object를 생성
		 * 
		 * @param
		 * @return 공지사항 Value Object
		 */
		 private function parseContract(src:ArrayCollection):ArrayCollection
		{
			var list:ArrayCollection = new ArrayCollection;
			
			for each(var rec:Object in src)
			{
				var vo:ContractVO = new ContractVO();
				
				vo.PERNR		= StringUtil.trimFront(rec.PERNR, "0");			//사번
				vo.ENAME		= rec.ENAME;		//성명
				vo.STEXT		= rec.STEXT;		//부서명
				vo.PLANST		= rec.PLANST;		//내용
				vo.PTEXT		= rec.PTEXT;		//게시일자(수정일자)
				vo.STELLT		= rec.STELLT;		//사원 또는 지원자의 포맷된 이름
				vo.JWTXT		= rec.JWTXT;		//조회수
				vo.TRFGR		= rec.TRFGR;		//파일이름
				vo.TRFST		= rec.TRFST;
				vo.COMNM		= rec.COMNM;
				vo.CTTYP_TX		= rec.CTTYP_TX;
				vo.BEGDA		= DateUtil.stringToDateTime(rec.BEGDA);		//시작일
				vo.ENDDA		= DateUtil.stringToDateTime(rec.ENDDA);		//종료일
				list.addItem(vo);
			}
			return list;
		}
	}
}